package com.itheima.TCP.TCP接收协议;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class SocketThread implements Runnable{
    private Socket accept;

    public SocketThread(Socket socket){
        this.accept=socket;
    }
    @Override
    public void run() {
      try{

          //网络中的流，从客户端读取数据的
          BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
          //本地IO流，把数据写到本地中，实现永久化存储
          //缓冲字节输出流
          // BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day629_网络编程入门\\1234"+System.currentTimeMillis()+".txt"));
          //使用UUDID产生随机文件名
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day629_网络编程入门\\" + UUID.randomUUID().toString() + ".txt"));
          byte[] bys = new byte[1024];
          int len;
          while ((len = bis.read(bys)) != -1) {
              bos.write(bys, 0, len);
              bos.flush();
          }

          BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
          bw.write("上传成功");
          bw.newLine();
          bw.flush();
          bos.close();
          accept.close();

      }catch (Exception e){
          e.printStackTrace();
      }
    }
}
